//
//  StatusViewModel.swift
//  WKweiBo
//
//  Created by 阿拉斯加的狗 on 15/12/11.
//  Copyright © 2015年 阿拉斯加的🐶. All rights reserved.
//

import UIKit

class StatusViewModel: CustomStringConvertible {
    
    var status: Status
    
    //定义cellID
    var cellID: String {
    
        //判断可重用标示符
        return status.retweeted_status != nil ? StatusCellRetweetedId : StatusCellNormalId
        
    }
    
    //计算行高
    lazy var rowHeight: CGFloat = {
    
        //定义cell
        var cell: StatusCell
        
        if self.status.retweeted_status != nil {
        
            // 1. cell
            cell = StatusRetweetedCell(style: .Default, reuseIdentifier: StatusCellRetweetedId)
        }else{
        
            cell = StatusNormalCell(style: .Default, reuseIdentifier: StatusCellNormalId)
        }
        
        
        
        
        // 2. 记录高度
        return cell.rowHeight(self)
    
    
    }()
    
    ///转发微博名
    var retweetedText: String? {
        
        guard let s = status.retweeted_status else{
            
            //没有转发微博
            return nil
            
        }
        
        return "@" + (s.user?.screen_name ?? "") + ":" + (s.text ?? "")
    }
    
    
    /// 用户头像 URL
    var userProfileUrl: NSURL {
        return NSURL(string: status.user?.profile_image_url ?? "")!
    }
    /// 用户默认头像
    var userDefaultIconView: UIImage {
        return UIImage(named: "avatar_default_big")!
    }
    /// 用户会员图标
    var userMemberImage: UIImage? {
        
        // 根据 mbrank 来生成图像
        if status.user?.mbrank > 0 && status.user?.mbrank < 7 {
            return UIImage(named: "common_icon_membership_level\(status.user!.mbrank)")
        }
        return nil
    }
    /// 用户认证图标
    /// 认证类型，-1：没有认证，0，认证用户，2,3,5: 企业认证，220: 达人
    var userVipImage: UIImage? {
        
        switch(status.user?.verified_type ?? -1) {
        case 0: return UIImage(named: "avatar_vip")
        case 2, 3, 5: return UIImage(named: "avatar_enterprise_vip")
        case 220: return UIImage(named: "avatar_grassroot")
        default: return nil
        }
    }
    
    //缩略图URL数组
    var thumbnailUrls: [NSURL]?
    

    
    init(status: Status) {
    
        self.status = status
        
        //获取转发微博或者原创微博的组图
        if let urls = status.retweeted_status?.pic_urls ?? status.pic_urls{
        
            //创建一个数组
            thumbnailUrls = [NSURL]()
            
            //遍历字典数组
            for dict in urls {
            
                let url = NSURL(string: dict["thumbnail_pic"]!)
                
                //加入到数组中
                thumbnailUrls?.append(url!)
            
            }
        
        }
    
    }

    
    //描述信息
    var description: String {
    
        return status.description + "配图数组 \(thumbnailUrls ?? [] as NSArray)"
    
    }
    
}